package com.project.controller.web;


import com.project.entity.Class;
import com.project.entity.Department;
import com.project.entity.Major;
import com.project.entity.School;
import com.project.myBeans.R;
import com.project.service.ClassService;
import com.project.service.DepartmentService;
import com.project.service.MajorService;
import com.project.service.SchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@EnableAutoConfiguration
@RestController
@RequestMapping(value = "/school")
@Api(description = "学院接口",value = "school")
public class SchoolController {

    @Autowired
    SchoolService schoolService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    MajorService majorService;

    @Autowired
    ClassService classService;

    /**
     * 2018/3/22
     * @param school
     * @return
     */
    @ApiOperation("添加学院")
    @PostMapping("/add")
    public R add(@RequestBody School school){
        try{
            schoolService.add(school);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation("院系专业班级结构树")
    @GetMapping("/getSchoolMajorClassTree/{rank}")
    public R getTree(@PathVariable Integer rank){
        try{
            if(rank >= 2) {
                List<School> schoolList = schoolService.findAll();
                for (School school : schoolList) {
                    school.setParentId(null);
                    school.setTreeKey("school"+school.getId());
                    if(rank >= 3) {
                        List<Department> departmentList = departmentService.findBySchoolid(school.getId());
                        for (Department department : departmentList) {
                            department.setTreeKey("department"+department.getId());
                            department.setParentId(school.getTreeKey());
                            if(rank >= 4) {
                                List<Major> majorList = majorService.findByDepartmentid(department.getId());
                                for (Major major : majorList) {
                                    major.setTreeKey("major"+major.getId());
                                    major.setParentId(department.getTreeKey());
                                    if(rank >= 5) {
                                        List<Class> classList = classService.findByMajorId(major.getId());
                                        for (Class clazz : classList) {
                                            clazz.setTreeKey("class"+clazz.getId());
                                            clazz.setParentId(major.getTreeKey());
                                        }
                                        major.setChildren(classList);
                                    }
                                }
                                department.setChildren(majorList);
                            }
                        }
                        school.setChildren(departmentList);
                    }
                }
                return R.ok().put("data",schoolList);
            }else {
                return R.error("无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 2018/5/30
     *
     * @param
     * @return
     * @author TateBrown
     */
    @ApiOperation("返回所有学院")
    @GetMapping("/findAllschool")
    public R findAllschool() {
        try {
            return R.ok("获取成功").put("data", schoolService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
}
